/*
 * Copyright 2009 Cedric Priscal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package com.zhxh.nzpos.serialport;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

import szxb.test.ll.jni.test4052;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.EditText;
import android_serialport_api.SerialPort;

import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;

/**
 * 读取卡信息父类
 * 
 * @author wangzt
 * 
 */
public abstract class ReadCardNumber extends Activity {

	protected EditText cardET;
	//读取身份证信息
	protected byte[] mValueRead20 = { (byte) 0x9b, (byte) 0x0e, (byte) 0x00, (byte) 0x03,
			(byte) 0x32, (byte) 0x02,(byte) 0x00 , (byte) 0xa6 };
	protected byte[] mValueRead21 = { (byte) 0x9b, (byte) 0x0e, (byte) 0x00, (byte) 0x03,
			(byte) 0x32, (byte) 0x02,(byte) 0x01 , (byte) 0xa7 };
	protected byte[] mValueRead22 = { (byte) 0x9b, (byte) 0x0e, (byte) 0x00, (byte) 0x03,
			(byte) 0x32, (byte) 0x02,(byte) 0x02 , (byte) 0xa4 };
	protected byte[] mValueRead23 = { (byte) 0x9b, (byte) 0x0e, (byte) 0x00, (byte) 0x03,
			(byte) 0x32, (byte) 0x02,(byte) 0x03 , (byte) 0xa5 };
	//身份证号 
	protected String idcard1 = "";
	protected String idcard2 = "";
	//积分
	protected String points = "";
	// 身份证号
	
	protected String card = "";

	protected byte[] mValueToSend = { (byte) 0x9b, (byte) 0x0c, (byte) 0x00, (byte) 0x02,
			(byte) 0x30, (byte) 0x52, (byte) 0xf7 };

	protected byte[] mValueToChk1 = { (byte) 0x9b, (byte) 0x0d, (byte) 0x00, (byte) 0x09,
			(byte) 0x31, (byte) 0x60, (byte) 0x01, (byte) 0xff, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xcf };

	protected byte[] mValueToCheck2 = { (byte) 0x9b, (byte) 0x0d, (byte) 0x00,
			(byte) 0x09, (byte) 0x31, (byte) 0x60, (byte) 0x02, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
			(byte) 0xcc };
	byte[] mValueToCheck3 = { (byte) 0x9b, (byte) 0x0d, (byte) 0x00,
			(byte) 0x09, (byte) 0x31, (byte) 0x60, (byte) 0x03, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
			(byte) 0xcd };
	byte[] mValueToCheck4 = { (byte) 0x9b, (byte) 0x0d, (byte) 0x00,
			(byte) 0x09, (byte) 0x31, (byte) 0x60, (byte) 0x04, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
			(byte) 0xca };
	byte[] mValueToCheck5 = { (byte) 0x9b, (byte) 0x0d, (byte) 0x00,
			(byte) 0x09, (byte) 0x31, (byte) 0x60, (byte) 0x05, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
			(byte) 0xcb };

	protected SerialPort mSerialPort;
	protected OutputStream mOutputStream;
	protected InputStream mInputStream;
	protected boolean mByteReceivedBack;
	protected Object mByteReceivedBackSemaphore = new Object();


	protected byte[] mReciveData = new byte[128];
	protected int j = 0;

	

	protected abstract void notifyActivity();
	
	protected boolean xunka(byte[] buffer, int size) {
		boolean flag = false;
		synchronized (mByteReceivedBackSemaphore) {
			int i;
			for (i = 0; i < size; i++) {
				mReciveData[i] = buffer[i];
			}

			String result = String.format("%02x%02x", mReciveData[0],
					mReciveData[1]);
			if ("9b0c".equalsIgnoreCase(result)) {

				String success = String.format("%02x", mReciveData[5]);
				if ("00".equalsIgnoreCase(success)) {
					card = String.format(
							"%02x%02x%02x%02x",
							mReciveData[9], mReciveData[10], mReciveData[11],
							mReciveData[12]);
					flag = true;
				}

			}
		}
		return flag;
	}

	protected boolean yanzheng(byte[] buffer, int size) {
		boolean flag = false;
		synchronized (mByteReceivedBackSemaphore) {
			int i;
			for (i = 0; i < size; i++) {
				mReciveData[i] = buffer[i];
			}

			String result = String.format("%02x%02x", mReciveData[0],
					mReciveData[1]);
			if ("9b0d".equalsIgnoreCase(result)) {
				String success = String.format("%02x", mReciveData[5]);
				if ("00".equalsIgnoreCase(success)) {
					flag = true;
				}
			}
		}
		return flag;
	}

	protected String duka(byte[] buffer, int size) {
		String data = "";
		synchronized (mByteReceivedBackSemaphore) {
			int i;
			for (i = 0; i < size; i++) {
				mReciveData[i] = buffer[i];
			}

			String result = String.format("%02x%02x", mReciveData[0],
					mReciveData[1]);
			if ("9b0e".equalsIgnoreCase(result)) {

				String success = String.format("%02x", mReciveData[5]);

				if ("00".equalsIgnoreCase(success)) {
					for (int m = 6; m < 22; m++) {
						data += String.format("%02x", mReciveData[m]);
					}
				}
			}
		}
		return data;
	}

	protected String xieshuju(byte[] buffer, int size) {
		String data = "";
		synchronized (mByteReceivedBackSemaphore) {
			int i;
			for (i = 0; i < size; i++) {
				mReciveData[i] = buffer[i];
			}

			String result = String.format("%02x%02x", mReciveData[0],
					mReciveData[1]);
			if ("9b0f".equalsIgnoreCase(result)) {
				String success = String.format("%02x", mReciveData[5]);

				if ("00".equalsIgnoreCase(success)) {
					for (int m = 6; m < 22; m++) {
						data += String.format("%02x", mReciveData[m]);
					}
				}
			}
		}
		return data;
	}

	private void DisplayError(int resourceId) {
		AlertDialog.Builder b = new AlertDialog.Builder(this);
		b.setTitle("Error");
		b.setMessage(resourceId);
		b.setPositiveButton("OK", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// 在手机上操作。暂时注释掉
				// ReadSerialPort.this.finish();
			}
		});
		b.show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			test4052.testopen();
			test4052.testioctl(0);
			mSerialPort = new SerialPort(new File(Constants.SERIAL_PATH),
					Constants.SERIAL_BOUNDRATE, 0);
			mOutputStream = mSerialPort.getOutputStream();
			mInputStream = mSerialPort.getInputStream();

			/* Create a receiving thread */
			// mReadThread = new ReadThread();
			// mReadThread.start();


		} catch (SecurityException e) {
			DisplayError(R.string.error_security);
		} catch (IOException e) {
			DisplayError(R.string.error_unknown);
		} catch (InvalidParameterException e) {
			DisplayError(R.string.error_configuration);
		}
	}


	@Override
	protected void onDestroy() {

		mSerialPort = null;
		super.onDestroy();
	}
}
